//
//  Date+YHExtension.swift
//  Adorbee
//
//  Created by amovision on 2024/12/27.
//

import Foundation

public extension Date {
  var pastTimeFormat: String {
    let seconds = Int(Date().timeIntervalSince(self))
    return if seconds < 30 { "Just".local }
    else if seconds < 60 { String("%d seconds ago".local, seconds) }
    else if seconds < 3600 { String("%d minutes ago".local, seconds/60) }
    else if seconds < 3600*24 { String("%d hours ago".local, seconds/3600) }
    else if seconds < 3600*24*7 { String("%d days ago".local, seconds/3600/24) }
    else if seconds < 3600*24*30 { String("%d weeks ago".local, seconds/3600/24/7) }
    else if seconds < 3600*24*365 { String("%d months ago".local, seconds/3600/24/30) }
    else { String("%d years ago".local, seconds/3600/24/365) }
  }

  var timestamp: Int {
    Int(timeIntervalSince1970)
  }

  var milliTimestamp: Int {
    Int(timeIntervalSince1970*1000)
  }
}

public extension Int {
  var date: Date {
    Date(timeIntervalSince1970: TimeInterval(self))
  }
  
  func dateString(_ format: String, locale: Locale = .current) -> String {
    let date = Date(timeIntervalSince1970: TimeInterval(self))
    let formatter = DateFormatter()
    formatter.dateFormat = format
    formatter.locale = locale
    return formatter.string(from: date)
  }
}
